<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\helper;

use builder\base\InvalidInstanceException;
use builder\table\HelperInterface;
use builder\table\util\RowOperationHelper;
use builder\table\util\TableHeadHelper;
use builder\table\util\ToolbarCustomHelper;
use builder\table\util\ToolbarFilterHelper;
use builder\table\util\ToolbarTab;

/**
 * 表格构建助手
 * @author cleverstone
 * @since ym2.0
 */
class Table
{
    /**
     * @var HelperInterface
     */
    private static $_helper;

    /**
     * 表头助手实例
     * @return TableHeadHelper
     * @throws InvalidInstanceException
     */
    public static function tableHead()
    {
        return self::get(TableHeadHelper::class);
    }

    /**
     * 行操作助手实例
     * @return RowOperationHelper
     * @throws InvalidInstanceException
     */
    public static function rowOperation()
    {
        return self::get(RowOperationHelper::class);
    }

    /**
     * 自定义工具项助手实例
     * @return ToolbarCustomHelper
     * @throws InvalidInstanceException
     */
    public static function toolbarCustom()
    {
        return self::get(ToolbarCustomHelper::class);
    }

    /**
     * 工具筛选项助手实例
     * @return ToolbarFilterHelper
     * @throws InvalidInstanceException
     */
    public static function toolbarFilter()
    {
        return self::get(ToolbarFilterHelper::class);
    }

    /**
     * 工具栏选项卡选项助手实例
     * @return ToolbarTab
     */
    public static function toolbarTab()
    {
        return new ToolbarTab();
    }

    /**
     * @param string $class 助手类名
     * @return HelperInterface
     * @throws InvalidInstanceException
     */
    public static function get($class)
    {
        if (!empty(self::$_helper[$class])) {
            return self::$_helper[$class];
        }

        $object = new $class();
        if (!$object instanceof HelperInterface) {
            throw new InvalidInstanceException($class . ' must be instance of ' . HelperInterface::class);
        }

        self::$_helper[$class] = $object;

        return $object;
    }
}